package
{
	import flash.net.URLVariables;
	import flash.utils.ByteArray;

	public class StateFirst extends State
	{
		protected var objects:Array = new Array();
		public function StateFirst()
		{
			super();
		}

		public override function Init():void
		{
			var s:Vector.<ExternalResource> = new Vector.<ExternalResource>();
			s.push(new ExternalResource(CommonResource.host + "Popup.swf",ExternalResource.byteArray));
			s.push(new ExternalResource("http://profile.ak.fbcdn.net/hprofile-ak-snc4/161186_1783887292_1874209_q.jpg",ExternalResource.image));
			//s.push(new ExternalResource(CommonResource.host + "terrain/terrain.txt",ExternalResource.byteArray));
			FBGame.stateStack.Push(new StateLoading(s,objects,this));
		}
		public override function LoadComplete(success:Boolean):void
		{
			if(success)
			{
				CommonResource.mvDialog = objects[0];
				CommonResource.myAvatar = objects[1].bitmapData;
				/*
				var byteArray:ByteArray = objects[2];
				var data:Array = byteArray.readMultiByte(byteArray.bytesAvailable,"us-ascii").split("\r\n");
				for(var i:int = 0;i<data.length;i+=2)
				{
					CommonResource.terrainList.push(new Terrain(data[i],CommonResource.host + "terrain/" + data[i+1]));
				}
				*/
				var httppost:FBHttpPost = new FBHttpPost(PostCallback);
				httppost.Post("state=first&m1=GetUserInfo");
			}
			else
			{
				
			}
		}
		public override function Render(g:MyGraphics):void
		{
			g.Clear(0xFF000000);
		}
		public function PostCallback(data:String,success:Boolean):void
		{
			if(success == false)
			{
				FBGame.stateStack.Push(new StatePopup(data,"OK"));
				return;
			}
			var variables:URLVariables = new URLVariables(data);
			if (variables.result == 0)
			{
				CommonResource.myPlayer = new Player(variables);
				FBGame.stateStack.Pop();
				FBGame.stateStack.Push(new StateBattle(new Vector.<Unit>));
				//FBGame.stateStack.Push(new StateExplore(1));
			}
			else if(variables.error == "UserNotExist")
			{
				FBGame.stateStack.Pop();
				FBGame.stateStack.Push(new StateClass());
			}
			else
			{
				FBGame.stateStack.Push(new StatePopup(data,"OK"));
			}
		}
	}
}